package common.autoreview

import common.qiniu.QiniuContentSafe
import utils.LogUtil

abstract class ImageDetector: Detector<String>

/*
* 恐怖暴力图片审核
* */
class TerrorImageDetector: ImageDetector() {

    companion object{
        val TAG = TerrorImageDetector::class.simpleName!!
    }

    object TerrorLabel{
        const val NORMAL = 0
        const val TERROR = 1
    }

    override fun detect(key: String, chain: DetectorChain<String>): AutoReviewState {
        val result = QiniuContentSafe.pictureTerror(key) ?: return AutoReviewState.REVIEW
        LogUtil.i(TAG, result.toString())
        //如果七牛云检测不成功或者检测结果需要人工review
        if (!result.isSuccess() || result.review){
            return AutoReviewState.REVIEW
        }
        return if (result.label == TerrorLabel.NORMAL){
            // 如果识别为正常图片,并且可信度超过AutoReview的阈值
            if (result.rate >= AutoReviewConfig.TERROR_PASS_THRESHOLD){
                chain.next(key)
            } else {
                AutoReviewState.REVIEW
            }
            // 如果识别为恐暴图片,并且可信度超过AutoReview的阈值
        } else if (result.rate >= AutoReviewConfig.TERROR_UNPASS_THRESHOLD){
            AutoReviewState.UNPASS
        }else {
            AutoReviewState.REVIEW
        }
    }
}

/*
* 色情图片审核
* */
class NropImageDetector: ImageDetector(){

    companion object{
        val TAG = NropImageDetector::class.simpleName!!
    }
    object NropLabel {
        const val PORN = 0
        const val SEXY = 1
        const val NORMAL = 2
        const val ERROR = -1
    }

    override fun detect(key: String, chain: DetectorChain<String>): AutoReviewState {
        val result = QiniuContentSafe.picturenrop(key) ?: return AutoReviewState.REVIEW
        LogUtil.i(TAG, result.toString())

        //如果七牛云检测不成功或者检测结果需要人工review
        if (!result.isSuccess() || result.review){
            return AutoReviewState.REVIEW
        }
        when(result.label){
            NropLabel.NORMAL -> {
                return if (result.rate >= AutoReviewConfig.NROP_PASS_THRESHOLD){
                    chain.next(key)
                } else {
                    AutoReviewState.REVIEW
                }
            }
            NropLabel.ERROR -> {
                return if (result.rate >= AutoReviewConfig.NROP_UNPASS_THRESHOLD) {
                    AutoReviewState.UNPASS
                } else {
                    AutoReviewState.REVIEW
                }
            }
            NropLabel.PORN, NropLabel.SEXY -> {
                return if (result.rate >= AutoReviewConfig.NROP_UNPASS_THRESHOLD) {
                    AutoReviewState.UNPASS
                } else {
                    AutoReviewState.REVIEW
                }
            }
        }
        //未知Label
        return AutoReviewState.REVIEW
    }
}



